﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using PreguntaAlExperto.DataFramework;
using PreguntaAlExperto.RulesLayer.Entities;
using PreguntaAlExperto.TransporterObjectsFramework;
using PreguntaAlExperto.RulesLayer.ObjectMapper.Contracts;

namespace PreguntaAlExperto.ObjectMapper.DTO
{
    internal class PublisherDTO
        : BaseDTO
    {
        public object Select(string query, SearchCriteria searchCriteria)
        {
            DataAccess dataAccess = new DataAccess();
            BaseList<Publisher> _transporter = new BaseList<Publisher>();

            foreach (object rawEntity in dataAccess.Select(query, searchCriteria))
            {
                Publisher _publisher = new Publisher();
                switch (query)
                { 
                    case "SQL_SELECT_PUBLISHER":
                        _publisher.ID_Publisher = int.Parse(((ArrayList)rawEntity)[0].ToString());
                        _publisher.Name = ((ArrayList)rawEntity)[1].ToString();
                        _publisher.Member = (bool)((ArrayList)rawEntity)[2];

                        SearchCriteria _searchCriteria = new SearchCriteria();
                        _searchCriteria.addParameter("@LoginDate", DateTime.Now);
                        _searchCriteria.addParameter("@PublisherID", _publisher.ID_Publisher);
                        dataAccess.Actualize("SQL_UPDATE_PUBLISHER_LOGIN_DATE", _searchCriteria);

                        break;
                    default:
                        _publisher.ID_Publisher = int.Parse(((ArrayList)rawEntity)[0].ToString());
                        _publisher.Name = ((ArrayList)rawEntity)[1].ToString();
                        _publisher.Username = ((ArrayList)rawEntity)[2].ToString();
                        _publisher.Password = ((ArrayList)rawEntity)[3].ToString();
                        _publisher.Email = ((ArrayList)rawEntity)[4].ToString();
                        _publisher.Member = (bool)((ArrayList)rawEntity)[5];
                        break;
                }
                _transporter.Add(_publisher);
            }
            _transporter.TotalRecords = dataAccess.getRecordsAfected;
            return _transporter;
        }
    }
}
